'use strict';


const await = require('await-stream-ready/lib/await');
const baseController = require('../../utils/base')
class TableController extends baseController {
    async index() {


        var sql = 'select * from `table`'
        let result = await this.app.mysql.query(sql)
        await this.ctx.render('admin/table/index', {
            list: result
        })
    }
    async add() {
        await this.ctx.render('admin/table/add', {})
    }
    async doAdd() {

        let { title, tableNum, status } = this.ctx.request.body
        var sql = 'insert into `table` (title, table_num, sort, status) VALUES (?,?,?,?)'

        let result = await this.app.mysql.query(sql, [title, tableNum, 1, status])


        if (result.protocol41) {
            await this.success("添加桌号成功", `/${this.config.pathname}/table`);
        } else {
            await this.error("添加桌号失败", `/${this.config.pathname}/table`);
        }

    }
    async edit() {
        let { id } = this.ctx.query

        let result = await this.app.mysql.query('select * from `table` where id = ?', id)


        await this.ctx.render('admin/table/edit', {
            list: result[0]
        })
        // this.ctx.body = this.ctx.query
    }
    async doEdit() {
        let { title, tableNum, status, id } = this.ctx.request.body
        var sql = 'update `table` set title = ?,table_num = ? , status=? where id = ?'
        this.app.mysql.query(sql, [title, tableNum, status, id])

        if (result.protocol41) {
            await this.success("编辑桌号成功", `/${this.config.pathname}/table`);
        } else {
            await this.error("编辑桌号失败", `/${this.config.pathname}/table`);
        }
    }
    async del() {
        let { id } = this.ctx.query
        let result = await this.app.mysql.query('delete from `table` where id=?', [id])

        if (result.protocol41) {
            await this.success("删除桌号成功", `/${this.config.pathname}/table`);
        } else {
            await this.error("删除桌号失败", `/${this.config.pathname}/table`);
        }

    }
    async svg() {

        let { id } = this.ctx.query
        let resultAll = await this.app.mysql.query('select * from `table` where id = ?',[id])
        console.log(resultAll[0].title);
        let result = await this.service.admin.utlis.createSvg()
        await this.ctx.render('admin/table/svg', {
            img: "http://" + result.Location,
            bgImg: '/public/admin/images/bg.jpg',
            text: resultAll[0].title
        })

        // this.ctx.body ="http:\\"+ result.Location
    }
}

module.exports = TableController;
